package com.sheyu.mall.product.service.attr.impl;

import com.sheyu.mall.product.dao.attr.AttrAttrgroupRelationDao;
import com.sheyu.mall.product.entity.attr.AttrAttrgroupRelationEntity;
import com.sheyu.mall.product.entity.attr.AttrEntity;
import com.sheyu.mall.product.service.attr.AttrService;
import com.sheyu.mall.product.service.attr.vo.AttrGroupwithAttrsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sheyu.common.utils.PageUtils;
import com.sheyu.common.utils.Query;

import com.sheyu.mall.product.dao.attr.AttrGroupDao;
import com.sheyu.mall.product.entity.attr.AttrGroupEntity;
import com.sheyu.mall.product.service.attr.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrAttrgroupRelationDao relationDao;

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByID(Map<String, Object> params, Long catelogId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();

        String key = (String)params.get("key");
        if (StringUtils.isNotBlank(key)){
            wrapper.and((obj ->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            }));
        }
        if (catelogId==0){
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }

        wrapper.eq("catelog_id",catelogId);
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<AttrEntity> queryAttrByAttrgroupId(Long attrgroupId) {
        QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("attr_group_id",attrgroupId);
        List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntitys = relationDao.selectList(queryWrapper);
        List<Long> ids  = attrAttrgroupRelationEntitys.stream().map(attrAttrgroupRelationEntity -> attrAttrgroupRelationEntity.getAttrId()
                ).collect(Collectors.toList());
        if (ids==null||ids.size()==0){
            return null;
        }
        List<AttrEntity> byIds = (List)attrService.listByIds(ids);
        return byIds;
    }

    @Override
    public List<AttrGroupwithAttrsVo> getAttrGroupAttrs(Long catelogId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
//        List<AttrGroupEntity> attrGroupEntityList = baseMapper.selectByCatelogIdBySQL(catelogId);
        List<AttrGroupwithAttrsVo> attrGroupwithAttrsVos = attrGroupEntities.stream().map((item)->{
            AttrGroupwithAttrsVo attrGroupwithAttrsVo = new AttrGroupwithAttrsVo();
            BeanUtils.copyProperties(item,attrGroupwithAttrsVo);
            List<AttrEntity> attrEntityList = queryAttrByAttrgroupId(item.getAttrGroupId());
            attrGroupwithAttrsVo.setAttrs(attrEntityList);
            return attrGroupwithAttrsVo;
        }).filter((obj)->{
            return obj.getAttrs() !=null;
        }).collect(Collectors.toList());
        return attrGroupwithAttrsVos;
    }

}
